package com.yhsoft.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yhsoft.benefitcard.controller.request.GetGoodsListInVo;
import com.yhsoft.benefitcard.controller.request.GetJFKGoodListInVo;
import com.yhsoft.benefitcard.controller.response.GetGoodsListOutVo;
import com.yhsoft.benefitcard.controller.response.GetJFKGoodListOutVo;
import com.yhsoft.benefitcard.entity.BatchGoods;
import com.yhsoft.benefitcard.entity.CardsInfo;
import com.yhsoft.benefitcard.service.IBatchGoodsService;
import com.yhsoft.benefitcard.service.ICardsInfoService;
import com.yhsoft.common.service.TokenService;
import com.yhsoft.goods.controller.Po.GoodSkuPo;
import com.yhsoft.goods.controller.Po.skuInfoPo;
import com.yhsoft.goods.controller.requset.*;
import com.yhsoft.goods.controller.response.*;
import com.yhsoft.goods.entity.*;
import com.yhsoft.goods.mapper.CompanySkuChargeMapper;
import com.yhsoft.goods.mapper.GoodSkuMapper;
import com.yhsoft.goods.mapper.GoodsInfoMapper;
import com.yhsoft.goods.mapper.SkuInfoMapper;
import com.yhsoft.goods.service.ICompanyGoodsService;
import com.yhsoft.goods.service.IGoodSkuService;
import com.yhsoft.goods.service.IGoodsInfoService;
import com.yhsoft.goods.service.ISkuInfoService;
import com.yhsoft.mall.controller.request.GoodsBarGoodsListInVo;
import com.yhsoft.sys.entity.SearchHistory;
import com.yhsoft.sys.service.ISearchHistoryService;
import com.yhsoft.wxprogram.controller.request.GoodsIdsInVo;
import com.yhsoft.wxprogram.controller.response.BenefitCardOutVo;
import com.yhsoft.wxprogram.po.GoodsPo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 商品信息表 服务实现类
 * </p>
 *
 * @author yyyyyyyh
 * @since 2023-09-22
 */
@Service
@Slf4j
public class GoodsInfoServiceImpl extends ServiceImpl<GoodsInfoMapper, GoodsInfo> implements IGoodsInfoService {

    @Resource
    private ISkuInfoService iSkuInfoService;

    @Resource
    private IGoodSkuService iGoodSkuService;

    @Resource
    private ICompanyGoodsService iCompanyGoodsService;

    @Resource
    private IBatchGoodsService iBatchGoodsService;

    @Resource
    private ICardsInfoService iCardsInfoService;

    @Resource
    private ISearchHistoryService iSearchHistoryService;

    @Resource
    private SkuInfoMapper skuInfoMapper;

    @Resource
    private GoodSkuMapper goodSkuMapper;

    @Resource
    private CompanySkuChargeMapper companySkuChargeMapper;

    @Override
    public IPage<AllGoodsOutVo> getAllGoods(AllGoodsInVo inVo, Long pageNo, Long pageSize) {
        QueryWrapper<GoodsInfo> wrapper = new QueryWrapper<>();
        wrapper.like(StringUtils.hasLength(inVo.getGoodsName()), "goods_name", inVo.getGoodsName())
                .like(inVo.getGoodsId() != null, "id", inVo.getGoodsId())
                .like(StringUtils.hasLength(inVo.getVendorName()), "vendorName", inVo.getVendorName())
                .ge(inVo.getMinStock() != null, "stock", inVo.getMinStock())
                .le(inVo.getMaxStock() != null, "stock", inVo.getMaxStock())
                .ge(inVo.getMinSkuPrice() != null, "minprice", inVo.getMinSkuPrice())
                .le(inVo.getMaxSkuPrice() != null, "maxprice", inVo.getMaxSkuPrice())
                .ge(inVo.getMinAdvocatePrice() != null, "minAdvocatePrice", inVo.getMinAdvocatePrice())
                .le(inVo.getMaxAdvocatePrice() != null, "maxAdvocatePrice", inVo.getMaxAdvocatePrice())
                .eq(inVo.getGoodsType() != null, "goods_type", inVo.getGoodsType())
                .eq(inVo.getStatusSysUp() != null, "status_sys_up", inVo.getStatusSysUp())
                .eq(inVo.getStatusVerify() != null, "status_verify", inVo.getStatusVerify());
        wrapper.ne("status_verify", -1);
        // 判断用户是平台/渠道商/供应商
        String sysType = TokenService.getUserBytoken().getSysType();
        if ("saler".equals(sysType)) {
            wrapper.inSql("id",
                    "select goods_id as id from g_company_goods where deleted = 0 and company_id = " + TokenService.getUserBytoken().getCompanyId());
            wrapper.eq("status_sys_up", 1)
                    .eq("status_verify", 1);
        }
        // wrapper.between(true,"a.vale","","")
        Page<AllGoodsOutVo> page = new Page<>(pageNo, pageSize);
        IPage<AllGoodsOutVo> iPage = this.baseMapper.getAllListPageByConditions(page, wrapper);
        return iPage;
    }

    @Override
    public IPage<GoodsInfoListByCompanyIdOutVo> getGoodsListByGId(GoodsInfoListByCompanyIdInVo inVo, Long pageNo,
                                                                  Long pageSize) {
        QueryWrapper<GoodsInfo> wrapper = new QueryWrapper<>();
        wrapper.like(StringUtils.hasLength(inVo.getGoodsName()), "goods_name", inVo.getGoodsName())
                .like(inVo.getGoodsId() != null, "id", inVo.getGoodsId())
                .like(StringUtils.hasLength(inVo.getVendorName()), "vendorName", inVo.getVendorName())

                .ge(inVo.getMinStock() != null, "stock", inVo.getMinStock())
                .le(inVo.getMaxStock() != null, "stock", inVo.getMaxStock())

                .ge(inVo.getMinSkuPrice() != null, "minprice", inVo.getMinSkuPrice())
                .le(inVo.getMaxSkuPrice() != null, "maxprice", inVo.getMaxSkuPrice())

                .ge(inVo.getMinChargeSys() != null, "sharge", inVo.getMinChargeSys())
                .le(inVo.getMaxChargeSys() != null, "sharge", inVo.getMaxChargeSys())

                .ge(inVo.getMinJscb() != null, "minJscb", inVo.getMinJscb())
                .le(inVo.getMaxJscb() != null, "maxJscb", inVo.getMaxJscb())

                .eq(inVo.getGoodsType() != null, "goods_type", inVo.getGoodsType())
                .eq(inVo.getDistributorId() != null, "distributorId", inVo.getDistributorId())
                .inSql("id",
                        "select goods_id as id from g_company_goods where deleted = 0 and company_id = " + inVo.getDistributorId());
        Page<GoodsInfoListByCompanyIdOutVo> page = new Page<>(pageNo, pageSize);
        IPage<GoodsInfoListByCompanyIdOutVo> iPage = this.baseMapper.getListPageByConditions(page, wrapper);
        return iPage;
    }

    @Override
    public IPage<GetGoodsListOutVo> getNotSelectedGoodsList(GetGoodsListInVo inVo, Long pageNo, Long pageSize) {
        Integer distributorId = TokenService.getUserBytoken().getCompanyId();
        QueryWrapper<GoodsInfo> wrapper = new QueryWrapper<>();
        wrapper.like(StringUtils.hasLength(inVo.getGoodsName()), "goods_name", inVo.getGoodsName())
                .like(inVo.getGoodsId() != null, "id", inVo.getGoodsId())
                .ge(inVo.getMinSkuPrice() != null, "minprice", inVo.getMinSkuPrice())
                .le(inVo.getMaxSkuPrice() != null, "maxprice", inVo.getMaxSkuPrice())
                .eq(inVo.getGoodsType() != null, "goods_type", inVo.getGoodsType())
                .eq("distributorId", distributorId)
                .inSql("id", "select goods_id as id from g_company_goods where deleted = 0 and status_mall_up =1 and " +
                        "company_id = " + distributorId)
                .notInSql("id", "select goods_id as id from benefit_batch_goods where deleted = 0 and " +
                        "second_bantch_num = '" + inVo.getBatch2Num() + "'");
        Page<GetGoodsListOutVo> page = new Page<>(pageNo, pageSize);
        IPage<GetGoodsListOutVo> iPage = this.baseMapper.getNotSelectedListPageByConditions(page, wrapper);
        for (GetGoodsListOutVo record : iPage.getRecords()) {
            GoodsInfo goodsInfo = baseMapper.selectById(record.getId());
            if (goodsInfo != null && goodsInfo.getPicUrl() != null) {
                record.setPicPath(goodsInfo.getPicUrl());
            }
        }
        return iPage;
    }

    @Override
    public IPage<GetJFKGoodListOutVo> getJFKSelectedGoodsList(GetJFKGoodListInVo inVo, String batch2Num,
                                                              Integer goodsType, Long pageNo, Long pageSize) {
        Integer distributorId = inVo.getDistributorId();
        if (inVo.getDistributorId() == null) {
            distributorId = TokenService.getUserBytoken().getCompanyId();
        }

        QueryWrapper<GoodsInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("goods_type", goodsType);
        if (StringUtils.hasLength(inVo.getGoodsName())) {
            wrapper.like("goods_name", inVo.getGoodsName());
        }
        wrapper.eq(inVo.getGoodsId() != null, "id", inVo.getGoodsId())
                .like(inVo.getVendorName() != null, "vendorName", inVo.getVendorName());

        wrapper.ge(inVo.getMinJscb() != null, "minJscb", inVo.getMinJscb())
                .le(inVo.getMaxJscb() != null, "maxJscb", inVo.getMaxJscb());

        wrapper.ge(inVo.getMinAdvocatePrice() != null, "minAdvocatePrice", inVo.getMinAdvocatePrice())
                .le(inVo.getMaxAdvocatePrice() != null, "maxAdvocatePrice", inVo.getMaxAdvocatePrice());

        wrapper.eq("distributorId", distributorId)
                .inSql("id", "select goods_id as id from g_company_goods where deleted = 0 and status_mall_up =1 and " +
                        "company_id = " + distributorId)
                .inSql("id", "select goods_id as id from benefit_batch_goods where deleted = 0 and second_bantch_num " +
                        "= '" + batch2Num + "'");
        Page<GetJFKGoodListOutVo> page = new Page<>(pageNo, pageSize);
        IPage<GetJFKGoodListOutVo> iPage = this.baseMapper.getSelectedListPageByConditions(page, wrapper);
        QueryWrapper<BatchGoods> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("second_bantch_num", batch2Num);
        List<BatchGoods> batchGoods = this.baseMapper.getBatchGoods(wrapper1);
        iPage.getRecords().forEach(e -> {
            batchGoods.forEach(a -> {
                if (e.getId().equals(a.getGoodsId())) {
                    e.setAddPrice(a.getAddPrice());
                }
            });
        });
        return iPage;
    }

    @Override
    public IPage<GoodsPo> getGoodsListByCard(String goodName, String goodsType, Long pageNo, Long pageSize) {

        // 添加到搜索历史
        if (StringUtils.hasLength(goodName)) {
            this.saveSearchHistory(goodName);
        }

        // 判断当前使用福利卡种类
        BenefitCardOutVo cardsInfo = iCardsInfoService.getInUseCard();
        if (cardsInfo == null) {
            IPage<GoodsPo> iPage = new Page<>(pageNo, pageSize);
            return iPage;
        }

        CardsInfo cardsInfo1 = iCardsInfoService.getById(cardsInfo.getId());
        String batch2Num = cardsInfo1.getSecondBantchNum();
        if ("JFK".equals(cardsInfo.getCardType())) {
        }

        // 获取到当前福利卡批次可售商品清单 已计算平台服务费
        // 价格 = 原价 + 平台服务费 + 加价
        Integer distributorId = TokenService.getUserBytoken().getCompanyId();
        QueryWrapper<GoodsInfo> wrapper = new QueryWrapper<>();
        wrapper.in(goodsType != null, "goods_type", goodsType)
                .like(StringUtils.hasLength(goodName), "goods_name", goodName)
                .eq("company_id", distributorId)
                .eq("second_bantch_num", batch2Num)
                .inSql("id", "select goods_id as id from g_company_goods where deleted = 0 and status_mall_up =1 and " +
                        "company_id = " + distributorId)
                .inSql("id", "select goods_id as id from benefit_batch_goods where deleted = 0 and second_bantch_num " +
                        "= '" + batch2Num + "'");
        Page<GoodsPo> page = new Page<>(pageNo, pageSize);
        IPage<GoodsPo> iPage = this.baseMapper.getWxGoodsListPageByConditions(page, wrapper);

        // 价格数据库计算
//        QueryWrapper<BatchGoods> wrapper1 = new QueryWrapper<>();
//        wrapper1.eq("second_bantch_num", batch2Num);
//        List<BatchGoods> batchGoods = this.baseMapper.getBatchGoods(wrapper1);
//        iPage.getRecords().forEach(e -> {
//            batchGoods.forEach(a -> {
//                if (e.getGoodId().equals(a.getGoodsId())) {
//                    BigDecimal price = new BigDecimal(e.getPrice());
//                    BigDecimal addPrice = a.getAddPrice();
//                    e.setPrice(price.add(addPrice).toString());
//                }
//            });
//        });

        return iPage;

    }

    private void saveSearchHistory(String goodName) {
        // 查询是否存在
        LambdaQueryWrapper wrapper = new LambdaQueryWrapper<SearchHistory>()
                .eq(SearchHistory::getUserId, TokenService.getUserBytoken().getId())
                .eq(SearchHistory::getKeyword, goodName);
        List<SearchHistory> list1 = iSearchHistoryService.getBaseMapper().selectList(wrapper);
        if (list1.size() == 0) {
            SearchHistory searchHistoryVo = new SearchHistory();
            searchHistoryVo.setKeyword(goodName);
            searchHistoryVo.setUserId(TokenService.getUserBytoken().getId());
            searchHistoryVo.setSource("wx");
            iSearchHistoryService.getBaseMapper().insert(searchHistoryVo);
        }
    }

    @Override
    public IPage<DisGoodsOutVo> getDisGoods(DisGoodsInVo inVo, Long pageNo, Long pageSize) {
        Integer distributorId = TokenService.getUserBytoken().getCompanyId();
        QueryWrapper<GoodsInfo> wrapper = new QueryWrapper<>();
        wrapper
                //商品名称
                .like(StringUtils.hasLength(inVo.getGoodsName()), "goods_name", inVo.getGoodsName())
                //商品id
                .like(inVo.getGoodsId() != null, "id", inVo.getGoodsId())
                //最少库存
                .ge(inVo.getMinStock() != null, "stock", inVo.getMinStock())
                //最多库存
                .le(inVo.getMaxStock() != null, "stock", inVo.getMaxStock())
                //最低销售价
                .ge(inVo.getMinSkuPrice() != null, "minprice", inVo.getMinSkuPrice())
                //最高销售价
                .le(inVo.getMaxSkuPrice() != null, "maxprice", inVo.getMaxSkuPrice())
                //最低利润率
                .ge(inVo.getMinMargin() != null, "minMargin", inVo.getMinMargin())
                //最高利润率
                .le(inVo.getMaxMargin() != null, "maxmargin", inVo.getMaxMargin())
                //商品类型
                .eq(inVo.getGoodsType() != null, "goods_type", inVo.getGoodsType())
                //商品状态
                // .eq(!(inVo.getStatusSysUp()==null),"statusSysUp",inVo.getStatusSysUp())
                //分销商编号
                .eq("distributorId", distributorId)
                .inSql("id",
                        "select goods_id as id from g_company_goods where deleted = 0 and company_id = " + distributorId);
        Page<DisGoodsOutVo> page = new Page<>(pageNo, pageSize);
        IPage<DisGoodsOutVo> iPage = this.baseMapper.getDisGoods(page, wrapper);
        return iPage;
    }

    @Override
    public GoodsOutVo getGoodInfoWx(Integer goodsId) {
        QueryWrapper<GoodsOutVo> wrapper = new QueryWrapper<>();
        wrapper.eq("a.id", goodsId);
        GoodsOutVo goodsOutVo = this.baseMapper.getGoodsInfoById(wrapper);
        List<GoodSkuPo> goodSkuPoList = iGoodSkuService.getGoodSkuByGoodsId(goodsId);
        List<skuInfoPo> skuInfoPoList = iSkuInfoService.getSkuInfoByGoodsId(goodsId);

        // 获取服务费
        LambdaQueryWrapper<CompanyGoods> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(CompanyGoods::getCompanyId, TokenService.getUserBytoken().getCompanyId());
        wrapper1.eq(CompanyGoods::getGoodsId, goodsId);
        List<CompanyGoods> companyGoodsList = iCompanyGoodsService.list(wrapper1);
        BigDecimal charge = BigDecimal.ZERO;
        if (companyGoodsList.size() > 0 && StringUtils.hasLength(companyGoodsList.get(0).getCustomCharge())) {
            charge = new BigDecimal(companyGoodsList.get(0).getCustomCharge()).divide(new BigDecimal(100));
        }

        // 获取加价
        BigDecimal addPrice = BigDecimal.ZERO;
        BenefitCardOutVo cardsInfo = iCardsInfoService.getInUseCard();
        if (cardsInfo != null) {
            CardsInfo cardsInfo1 = iCardsInfoService.getById(cardsInfo.getId());
            String batch2Num = cardsInfo1.getSecondBantchNum();
            QueryWrapper<BatchGoods> wrapper2 = new QueryWrapper<>();
            wrapper2.eq("second_bantch_num", batch2Num);
            wrapper2.eq("goods_id", goodsId);
            List<BatchGoods> batchGoods = this.baseMapper.getBatchGoods(wrapper2);
            if (batchGoods.size() > 0) {
                addPrice = batchGoods.get(0).getAddPrice();
            }
        }
        log.info("商品id：" + goodsId + "，加价：" + addPrice);

        List<BigDecimal> priceList = new ArrayList<>();
        for (GoodSkuPo goodSkuPo : goodSkuPoList) {
            List<skuInfoPo> skuInfoPos = new ArrayList<>();
            for (skuInfoPo skuInfoPo : skuInfoPoList) {
                if (goodSkuPo.getId().equals(skuInfoPo.getSkuNameId())) {
                    BigDecimal price = skuInfoPo.getSkuPrice();
                    price = price.multiply(BigDecimal.ONE.add(charge)).add(addPrice);
                    priceList.add(price);
                    skuInfoPo.setSkuPrice(price);
                    skuInfoPos.add(skuInfoPo);
                }
            }
            goodSkuPo.setSkuInfoList(skuInfoPos);
        }
        // 获取最小值
        if (priceList.size() > 0) {
            BigDecimal minPrice = Collections.min(priceList);
            goodsOutVo.setMinPrice(minPrice);
        }
        goodsOutVo.setGoodSkus(goodSkuPoList);
        return goodsOutVo;
    }

    @Override
    public IPage<AllGoodsOutVo> getVenderGoods(VenderGoodsInVo inVo, Long pageNo, Long pageSize) {
        QueryWrapper<GoodsInfo> wrapper = new QueryWrapper<>();
        wrapper.like(StringUtils.hasLength(inVo.getGoodsName()), "goods_name", inVo.getGoodsName())
                .ge(inVo.getMinSkuPrice() != null, "minprice", inVo.getMinSkuPrice())
                .le(inVo.getMaxSkuPrice() != null, "maxprice", inVo.getMaxSkuPrice())
                .ge(inVo.getMinMargin() != null, "minMargin", inVo.getMinMargin())
                .le(inVo.getMaxMargin() != null, "maxmargin", inVo.getMaxMargin())
                .eq("goods_type", 5)
                .eq(inVo.getStatusSysUp() != null, "status_sys_up", inVo.getStatusSysUp())
                .eq(inVo.getStatusVerify() != null, "status_verify", inVo.getStatusVerify());
        wrapper.ne("status_verify", -1);
        String type = TokenService.getUserBytoken().getSysType();
        if (type.equals("sys")) {
            if (StringUtils.hasLength(inVo.getVendorId())) {
                wrapper.eq("vendor_id", inVo.getVendorId());
            }
        }
        if (type.equals("vender")) {
            wrapper.eq("vendor_id", TokenService.getUserBytoken().getCompanyId());
        }

        Page<AllGoodsOutVo> page = new Page<>(pageNo, pageSize);
        IPage<AllGoodsOutVo> iPage = this.baseMapper.getAllListPageByConditions(page, wrapper);
        // 设置渠道商上架状态
//        iPage.getRecords().forEach(allGoodsOutVo -> {
//            LambdaQueryWrapper<CompanyGoods> wrapper1 = new LambdaQueryWrapper<>();
//            wrapper1.eq(CompanyGoods::getGoodsId, allGoodsOutVo.getId());
//            wrapper1.eq(CompanyGoods::getStatusMallUp, 1);
//
//            if (iCompanyGoodsService.getBaseMapper().exists(wrapper1)) {
//                allGoodsOutVo.setStatusSysUp(2);
//            }
//        });
        return iPage;
    }

    @Override
    public IPage<GoodsPo> getGoodsInfoListById(GoodsIdsInVo inVo, Long pageNo, Long pageSize) {
        // 获取商品合集
        String goodsIds = "-1";
        if (inVo.getGoodsIds() != null) {
            StringBuilder goodsIdsBuilder = new StringBuilder();
            goodsIdsBuilder.append(goodsIds);
            inVo.getGoodsIds().forEach(a -> {
                goodsIdsBuilder.append(",").append(a.getId());
            });
            goodsIds = goodsIdsBuilder.toString();
        }

        Integer distributorId = TokenService.getUserBytoken().getCompanyId();
        QueryWrapper<GoodsInfo> wrapper = new QueryWrapper<>();
        wrapper.inSql("id", goodsIds);
        wrapper.eq("company_id", distributorId);

        Page<GoodsPo> page = new Page<>(pageNo, pageSize);
        IPage<GoodsPo> iPage = this.baseMapper.getWxGoodsListByGoodsIds(page, wrapper);
        return iPage;
    }

    @Override
    public SkuInfoOutVo getSkuInfoOutVo(SkuInfoInVo inVo) {
        QueryWrapper<SkuInfoOutVo> wrapper = new QueryWrapper<>();
        wrapper.eq("a.id", inVo.getGoodsId());
        wrapper.eq("b.company_id", inVo.getDistributorId());
        wrapper.eq("b.deleted", 0);
        SkuInfoOutVo outVo = this.baseMapper.getSkuInfoOutVo(wrapper);
        return outVo;
    }

    @Override
    public GoodsOutVo getGoodInfoByGoodsId(Integer goodsId) {
        QueryWrapper<GoodsOutVo> wrapper = new QueryWrapper<>();
        wrapper.eq("a.id", goodsId);
        GoodsOutVo goodsOutVo = this.baseMapper.getGoodsInfoById(wrapper);
        List<GoodSkuPo> goodSkuPoList = iGoodSkuService.getGoodSkuByGoodsId(goodsId);
        List<skuInfoPo> skuInfoPoList = iSkuInfoService.getSkuInfoByGoodsId(goodsId);

        // 获取服务费
        BigDecimal charge = getCharge(goodsId);

        List<BigDecimal> priceList = new ArrayList<>();
        for (GoodSkuPo goodSkuPo : goodSkuPoList) {
            List<skuInfoPo> skuInfoPos = new ArrayList<>();
            for (skuInfoPo skuInfoPo : skuInfoPoList) {
                if (goodSkuPo.getId().equals(skuInfoPo.getSkuNameId())) {
                    // 计算价格
                    BigDecimal price = skuInfoPo.getSkuPrice();
                    price = price.multiply(BigDecimal.ONE.add(charge));
                    priceList.add(price);

                    skuInfoPo.setSkuPrice(price);
                    skuInfoPos.add(skuInfoPo);
                }
            }
            goodSkuPo.setSkuInfoList(skuInfoPos);
        }
        // 获取最小值
        if (priceList.size() > 0) {
            BigDecimal minPrice = Collections.min(priceList);
            goodsOutVo.setMinPrice(minPrice);
        }
        goodsOutVo.setGoodSkus(goodSkuPoList);
        return goodsOutVo;
    }


    private BigDecimal getCharge(Integer goodsId) {
        // 获取服务费
        LambdaQueryWrapper<CompanyGoods> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(CompanyGoods::getCompanyId, TokenService.getUserBytoken().getCompanyId());
        wrapper1.eq(CompanyGoods::getGoodsId, goodsId);
        List<CompanyGoods> companyGoodsList = iCompanyGoodsService.list(wrapper1);
        BigDecimal charge = BigDecimal.ZERO;
        if (companyGoodsList.size() > 0 && StringUtils.hasLength(companyGoodsList.get(0).getCustomCharge())) {
            charge = new BigDecimal(companyGoodsList.get(0).getCustomCharge()).divide(new BigDecimal(100));
        }
        return charge;
    }


    @Override
    public void addGoods(AddGoodsInVo inVo) {
        GoodsInfo goodsInfo = new GoodsInfo();
        BeanUtils.copyProperties(inVo, goodsInfo);
        goodsInfo.setGoodsType(5);
        goodsInfo.setVendorId(TokenService.getUserBytoken().getCompanyId());
        goodsInfo.setStatusVerify(-1);
        goodsInfo.setStatusSysUp(0);
        goodsInfo.setDeleted(0);
        this.getBaseMapper().insert(goodsInfo);
        Integer goodsId = goodsInfo.getId();

        if (inVo.getGoodSkus() != null && inVo.getGoodSkus().size() > 0) {
            inVo.getGoodSkus().forEach(a -> {
                GoodSku goodSku = new GoodSku();
                goodSku.setGoodsId(goodsId);
                goodSku.setSkuName(a.getSkuName());
                goodSkuMapper.insert(goodSku);
                Integer skuNameId = goodSku.getId();

                if (a.getSkuInfoList() != null && a.getSkuInfoList().size() > 0) {
                    a.getSkuInfoList().forEach(b -> {
                        SkuInfo skuInfo = new SkuInfo();
                        skuInfo.setGoodsId(goodsId);
                        skuInfo.setSkuNameId(skuNameId);
                        skuInfo.setSkuInfo(b.getSkuInfo());
                        skuInfo.setSkuPrice(b.getSkuPrice());
                        skuInfo.setStock(b.getStock());
                        skuInfoMapper.insert(skuInfo);
                    });
                }
            });
        }
    }

    @Override
    public void updateGoods(AddGoodsInVo inVo) {
        GoodsInfo goodsInfo = new GoodsInfo();
        BeanUtils.copyProperties(inVo, goodsInfo);
        this.getBaseMapper().updateById(goodsInfo);
        Integer goodsId = goodsInfo.getId();

        if (inVo.getGoodSkus() != null && inVo.getGoodSkus().size() > 0) {
            inVo.getGoodSkus().forEach(a -> {
                GoodSku goodSku = new GoodSku();
                goodSku.setGoodsId(goodsId);
                goodSku.setSkuName(a.getSkuName());
                Integer skuNameId = a.getId();

                if (a.getId() == null) {
                    goodSkuMapper.insert(goodSku);
                    skuNameId = goodSku.getId();
                } else {
                    goodSku.setId(a.getId());
                    goodSkuMapper.updateById(goodSku);
                }

                if (a.getSkuInfoList() != null && a.getSkuInfoList().size() > 0) {
                    Integer finalSkuNameId = skuNameId;
                    a.getSkuInfoList().forEach(b -> {
                        SkuInfo skuInfo = new SkuInfo();
                        skuInfo.setGoodsId(goodsId);
                        skuInfo.setSkuNameId(finalSkuNameId);
                        skuInfo.setSkuInfo(b.getSkuInfo());
                        skuInfo.setSkuPrice(b.getSkuPrice());
                        skuInfo.setStock(b.getStock());

                        if (b.getId() == null) {
                            skuInfoMapper.insert(skuInfo);
                        } else {
                            skuInfo.setId(b.getId());
                            skuInfoMapper.updateById(skuInfo);
                        }
                    });
                }
            });
        }
    }

    @Override
    public boolean exists(String goodsName) {
        LambdaQueryWrapper<GoodsInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(GoodsInfo::getGoodsName, goodsName);
        wrapper.eq(GoodsInfo::getGoodsType, 5);
        wrapper.eq(GoodsInfo::getVendorId, TokenService.getUserBytoken().getCompanyId());

        return this.getBaseMapper().exists(wrapper);
    }

    @Override
    public IPage<AllGoodsOutVo> getVenderGoodsList(VenderGoodsListInVo inVo, Long pageNo, Long pageSize) {

        QueryWrapper<GoodsInfo> wrapper = new QueryWrapper<>();
        wrapper.like(StringUtils.hasLength(inVo.getGoodsName()), "goods_name", inVo.getGoodsName())
                .like(inVo.getGoodsId() != null, "id", inVo.getGoodsId())
                .ge(inVo.getMinSkuPrice() != null, "minprice", inVo.getMinSkuPrice())
                .le(inVo.getMaxSkuPrice() != null, "maxprice", inVo.getMaxSkuPrice())
                .ge(inVo.getMinStock() != null, "stock", inVo.getMinStock())
                .le(inVo.getMaxStock() != null, "stock", inVo.getMaxStock())
                .eq("goods_type", 5)
                .eq(inVo.getStatusSysUp() != null, "status_sys_up", inVo.getStatusSysUp())
                .eq(inVo.getStatusVerify() != null, "status_verify", inVo.getStatusVerify());
        wrapper.eq("vendor_id", TokenService.getUserBytoken().getCompanyId());

        Page<AllGoodsOutVo> page = new Page<>(pageNo, pageSize);
        IPage<AllGoodsOutVo> iPage = this.baseMapper.getAllListPageByConditions(page, wrapper);
        return iPage;
    }

    @Override
    public IPage<GoodsPo> getGoodsListByCard_new(String goodName, String goodsTypes, Long pageNo, Long pageSize) {

        // 添加到搜索历史
        if (StringUtils.hasLength(goodName)) {
            this.saveSearchHistory(goodName);
        }

        IPage<GoodsPo> iPage = new Page<>(pageNo, pageSize);
        // 判断当前使用福利卡种类
        BenefitCardOutVo cardsInfo = iCardsInfoService.getInUseCard();
        // 若当前使用福利卡余额为0，则不显示商品
        if (cardsInfo == null || cardsInfo.getBalance().compareTo(BigDecimal.ZERO) == 0) {
            return iPage;
        }

        CardsInfo cardsInfo1 = iCardsInfoService.getById(cardsInfo.getId());
        String batch2Num = cardsInfo1.getSecondBantchNum();
        if (!cardsInfo.getCardType().equals("JFK")) {
            return iPage;
        }

        // 获取到当前福利卡批次可售商品清单，已计算服务费加价
        Integer distributorId = TokenService.getUserBytoken().getCompanyId();
        QueryWrapper<GoodsInfo> wrapper = new QueryWrapper<>();
        if (goodsTypes != null) {
            String[] goodsTypesArray = goodsTypes.split(",");
            wrapper.in("goods_type", goodsTypesArray);
        }
        wrapper.like(StringUtils.hasLength(goodName), "goods_name", goodName)
                .eq("distributorId", distributorId)
                .inSql("id", "select goods_id as id from g_company_goods where deleted = 0 and status_mall_up =1 and " +
                        "company_id = " + distributorId)
                .inSql("id", "select goods_id as id from benefit_batch_goods where deleted = 0 and second_bantch_num " +
                        "= '" + batch2Num + "'");
        Page<GoodsPo> page = new Page<>(pageNo, pageSize);
        iPage = this.baseMapper.getWxGoodsListPageByConditions_new(page, wrapper);

        // 价格计算
        // 价格 = 原价 + 平台服务费 + 加价(取最高价格)
        log.info("开始计算价格");
        // 获取最高加价
        Map<Integer, BigDecimal> addPriceMap = getAddPriceMap();

        LambdaQueryWrapper<CompanySkuCharge> wrapper3 = new LambdaQueryWrapper<>();
        wrapper3.eq(CompanySkuCharge::getCompanyId, TokenService.getUserBytoken().getCompanyId())
                .inSql(CompanySkuCharge::getGoodsId, "select goods_id as id from g_company_goods where deleted = 0 " +
                        "and status_mall_up =1 and " +
                        "company_id = " + distributorId)
                .inSql(CompanySkuCharge::getGoodsId, "select goods_id as id from benefit_batch_goods where deleted = " +
                        "0 and second_bantch_num " +
                        "= '" + batch2Num + "'");
        List<CompanySkuCharge> chargeList = companySkuChargeMapper.selectList(wrapper3);
        Map<Integer, BigDecimal> chargeMap = null;
        if (chargeList.size() > 0) {
            chargeMap = chargeList.stream().collect(Collectors.toMap(CompanySkuCharge::getSkuInfoId,
                    CompanySkuCharge::getMallAddPrice));
        }
        BigDecimal price;
        BigDecimal addPrice;
        BigDecimal addMallPrice;
        BigDecimal sumPrice;
        for (GoodsPo a : iPage.getRecords()) {
            // 有建议售价取建议售价，没建议售价，取结算成本
            log.info("商品：" + a.getGoodsName() + ",goodsId:" + a.getGoodId() + ",最小建议售价：" + a.getAdvocatePrice());
            log.info("商品：" + a.getGoodsName() + ",goodsId:" + a.getGoodId() + ",最小结算成本：" + a.getPrice());

            price = StringUtils.hasLength(a.getAdvocatePrice()) ? new BigDecimal(a.getAdvocatePrice()) :
                    new BigDecimal(a.getPrice());
            addPrice = addPriceMap.getOrDefault(a.getGoodId(), BigDecimal.ZERO);
            log.info("商品：" + a.getGoodsName() + ",goodsId:" + a.getGoodId() + ",最终福利卡加价是：" + addPrice + "元");

            addMallPrice = chargeMap == null ? BigDecimal.ZERO : chargeMap.getOrDefault(a.getSkuId(), BigDecimal.ZERO);
            log.info("商品：" + a.getGoodsName() + ",goodsId:" + a.getGoodId() + ",商城加价是：" + addMallPrice + "元");
            sumPrice = price.add(addMallPrice).add(addPrice);
            if (StringUtils.hasLength(a.getAdvocatePrice())) {
                log.info("商品：" + a.getGoodsName() + ",goodsId:" + a.getGoodId() + ",建议售价不为空，取建议售价");
                log.info("商品：" + a.getGoodsName() + ",goodsId:" + a.getGoodId() + ",最终售价是：" +
                        "最小建议售价(" + a.getAdvocatePrice() + ")+商城加价(" + addMallPrice + ") +最终福利卡加价(" + addPrice + ")=" + sumPrice + "元");
            } else {
                log.info("商品：" + a.getGoodsName() + ",goodsId:" + a.getGoodId() + ",建议售价为空！取结算成本");
                log.info("商品：" + a.getGoodsName() + ",goodsId:" + a.getGoodId() + ",最终售价是：" +
                        "最小结算成本(" + a.getPrice() + ")+商城加价(" + addMallPrice + ") +最终福利卡加价(" + addPrice + ")=" + sumPrice + "元");
            }
            a.setPrice(sumPrice.toString());
        }

        return iPage;
    }

    @Override
    public Map<Integer, BigDecimal> getAddPriceMap() {
        log.info(">>>>>>进入获取最高加价逻辑");
        // 规则,获取该用户所有绑定的福利卡的可售商品,对比正在使用的福利卡的商品清单进行比较,选最高价
        // 获取绑定的所有积分卡的商品
        QueryWrapper<BatchGoods> wrapper1 = new QueryWrapper<>();
        wrapper1.inSql("second_bantch_num",
                "select second_bantch_num from benefit_cards_info where balance > 0 and is_enable = 0 and user_id = '"
                        + TokenService.getUserBytoken().getId() + "'");
        wrapper1.eq("deleted", 0);
        List<BatchGoods> batchGoods = this.baseMapper.getBatchGoods(wrapper1);

        // 比较出最高加价
        Map<Integer, BigDecimal> addPriceMap = new HashMap<>();
        for (BatchGoods b : batchGoods) {
            if (!addPriceMap.containsKey(b.getGoodsId()) || addPriceMap.get(b.getGoodsId()).compareTo(b.getAddPrice()) < 0) {
                log.info("商品id：" + b.getGoodsId() + "在绑定积分卡批次：" + b.getSecondBantchNum() + "中可售,加价为" + b.getAddPrice() + "元");
                addPriceMap.put(b.getGoodsId(), b.getAddPrice());
            }
        }
        return addPriceMap;
    }

    @Override
    public IPage<GoodsPo> getGoodsInfoList_new(GoodsIdsInVo inVo, Long pageNo, Long pageSize) {

        // 获取绑定的所有积分卡的商品
        Map<Integer, BigDecimal> addPriceMap = getAddPriceMap();
        List<GoodsBarGoodsListInVo> list = inVo.getGoodsIds();

        // 取首页商品和所有绑定福利卡商品的交集
        addPriceMap.keySet().retainAll(list.stream().map(GoodsBarGoodsListInVo::getId).collect(Collectors.toSet()));

        List<GoodsBarGoodsListInVo> list1 = list.stream().filter(a -> addPriceMap.getOrDefault(a.getId(),
                BigDecimal.valueOf(-1)).compareTo(BigDecimal.valueOf(-1)) > 0).collect(Collectors.toList());

        if (list1.size() == 0) {
            return new Page<>(pageNo, pageSize);
        }

        // 获得该用户可以购买的商品合集
        String goodsIds = "-1";
        if (inVo.getGoodsIds() != null) {
            StringBuilder goodsIdsBuilder = new StringBuilder();
            goodsIdsBuilder.append(goodsIds);
            list1.forEach(a -> {
                goodsIdsBuilder.append(",").append(a.getId());
            });
            goodsIds = goodsIdsBuilder.toString();
        }

        log.info(goodsIds);

        LambdaQueryWrapper<CompanySkuCharge> wrapper3 = new LambdaQueryWrapper<>();
        wrapper3.eq(CompanySkuCharge::getCompanyId, TokenService.getUserBytoken().getCompanyId())
                .inSql(CompanySkuCharge::getGoodsId, goodsIds);
        List<CompanySkuCharge> chargeList = companySkuChargeMapper.selectList(wrapper3);
        Map<Integer, BigDecimal> chargeMap = null;
        if (chargeList.size() > 0) {
            chargeMap = chargeList.stream().collect(Collectors.toMap(CompanySkuCharge::getSkuInfoId,
                    CompanySkuCharge::getMallAddPrice));
        }

        Integer distributorId = TokenService.getUserBytoken().getCompanyId();
        QueryWrapper<GoodsInfo> wrapper = new QueryWrapper<>();
        wrapper.inSql("id", goodsIds);
        wrapper.eq("distributorId", distributorId);

        Page<GoodsPo> page = new Page<>(pageNo, pageSize);
        IPage<GoodsPo> iPage = this.baseMapper.getWxGoodsListPageByConditions_new(page, wrapper);

        BigDecimal price;
        BigDecimal addMallPrice;
        // 计算最高价
        for (GoodsPo a : iPage.getRecords()) {
            price = a.getAdvocatePrice() == null ? new BigDecimal(a.getPrice()) : new BigDecimal(a.getAdvocatePrice());
            addMallPrice = chargeMap == null ? BigDecimal.ZERO : chargeMap.getOrDefault(a.getSkuId(), BigDecimal.ZERO);

            BigDecimal addPrice = addPriceMap.getOrDefault(a.getGoodId(), BigDecimal.ZERO);
            log.info("商品id：" + a.getGoodId() + " 最终加价是：" + addPrice + "元");
            a.setPrice(price.add(addMallPrice).add(addPrice).toString());
            // 输出日志
            if (StringUtils.hasLength(a.getAdvocatePrice())) {
                log.info("商品：" + a.getGoodsName() + ",goodsId:" + a.getGoodId() + ",建议售价不为空，取建议售价");
                log.info("商品：" + a.getGoodsName() + ",goodsId:" + a.getGoodId() + ",最终售价是：" +
                        "最小建议售价(" + a.getAdvocatePrice() + ")+商城加价(" + addMallPrice + ") +最终福利卡加价(" + addPrice + ")=" + a.getPrice() + "元");
            } else {
                log.info("商品：" + a.getGoodsName() + ",goodsId:" + a.getGoodId() + ",建议售价为空！取结算成本");
                log.info("商品：" + a.getGoodsName() + ",goodsId:" + a.getGoodId() + ",最终售价是：" +
                        "最小结算成本(" + a.getPrice() + ")+商城加价(" + addMallPrice + ") +最终福利卡加价(" + addPrice + ")=" + a.getPrice() + "元");
            }
        }

        return iPage;
    }

    @Override
    public GoodsOutVo getGoodInfoWx_new(Integer goodsId) {
        QueryWrapper<GoodsOutVo> wrapper = new QueryWrapper<>();
        wrapper.eq("a.id", goodsId);
        GoodsOutVo goodsOutVo = this.baseMapper.getGoodsInfoById(wrapper);
        List<GoodSkuPo> goodSkuPoList = iGoodSkuService.getGoodSkuByGoodsId(goodsId);
        List<skuInfoPo> skuInfoPoList = iSkuInfoService.getSkuInfoByGoodsId(goodsId);

        // 获取服务费
        LambdaQueryWrapper<CompanyGoods> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(CompanyGoods::getCompanyId, TokenService.getUserBytoken().getCompanyId());
        wrapper1.eq(CompanyGoods::getGoodsId, goodsId);
        List<CompanyGoods> companyGoodsList = iCompanyGoodsService.list(wrapper1);
        BigDecimal charge = BigDecimal.ZERO;
        if (companyGoodsList.size() > 0 && StringUtils.hasLength(companyGoodsList.get(0).getCustomCharge())) {
            charge = new BigDecimal(companyGoodsList.get(0).getCustomCharge()).divide(new BigDecimal(100));
        }

        // 获取渠道商商品商城加价
        LambdaQueryWrapper<CompanySkuCharge> wrapper3 = new LambdaQueryWrapper<>();
        wrapper3.eq(CompanySkuCharge::getCompanyId, TokenService.getUserBytoken().getCompanyId())
                .eq(CompanySkuCharge::getGoodsId, goodsId);
        List<CompanySkuCharge> chargeList = companySkuChargeMapper.selectList(wrapper3);
        Map<Integer, BigDecimal> chargeMap = null;
        if (chargeList.size() > 0) {
            chargeMap = chargeList.stream().collect(Collectors.toMap(CompanySkuCharge::getSkuInfoId,
                    CompanySkuCharge::getMallAddPrice));
        }

        // 获取批次最高加价
        BigDecimal addPrice = this.getMaxAddPrice(goodsId);

        log.info("商品：" + goodsOutVo.getGoodsName() + ",商品id：" + goodsId + "，福利卡最高加价为：" + addPrice);

        List<BigDecimal> priceList = new ArrayList<>();

        BigDecimal price;
        BigDecimal addMallPrice;
        BigDecimal sumPrice;
        for (GoodSkuPo goodSkuPo : goodSkuPoList) {
            List<skuInfoPo> skuInfoPos = new ArrayList<>();
            for (skuInfoPo skuInfoPo : skuInfoPoList) {
                if (goodSkuPo.getId().equals(skuInfoPo.getSkuNameId())) {

                    // 有建议售价取建议售价，没建议售价，取结算成本
                    price = skuInfoPo.getAdvocatePrice() == null ?
                            skuInfoPo.getSkuPrice().multiply(BigDecimal.ONE.add(charge)) : skuInfoPo.getAdvocatePrice();
                    addMallPrice = chargeMap == null ? BigDecimal.ZERO : chargeMap.getOrDefault(skuInfoPo.getId(),
                            BigDecimal.ZERO);
                    log.info(goodsOutVo.getGoodsName() + "-" + skuInfoPo.getSkuInfo() + ",商品商城加价：" + addMallPrice);
                    sumPrice = price.add(addMallPrice).add(addPrice);

                    if (skuInfoPo.getAdvocatePrice() == null) {
                        log.info(goodsOutVo.getGoodsName() + "-" + skuInfoPo.getSkuInfo() + ",建议售价为空，取结算成本：" + skuInfoPo.getSkuPrice().multiply(BigDecimal.ONE.add(charge)) + "元");
                        log.info(goodsOutVo.getGoodsName() + "-" + skuInfoPo.getSkuInfo() + ",最终售价是：" +
                                "结算成本(" + skuInfoPo.getSkuPrice().multiply(BigDecimal.ONE.add(charge)) + ")+商城加价(" + addMallPrice + ") +最终福利卡加价(" + addPrice + ")=" + sumPrice + "元");
                    } else {
                        log.info(goodsOutVo.getGoodsName() + "-" + skuInfoPo.getSkuInfo() + ",建议售价不为空，取建议售价：" + skuInfoPo.getAdvocatePrice() + "元");
                        log.info(goodsOutVo.getGoodsName() + "-" + skuInfoPo.getSkuInfo() + ",最终售价是：" +
                                "建议售价(" + skuInfoPo.getAdvocatePrice() + ")+商城加价(" + addMallPrice + ") +最终福利卡加价(" + addPrice + ")=" + sumPrice + "元");
                    }

                    priceList.add(sumPrice);
                    skuInfoPo.setSkuPrice(sumPrice);
                    skuInfoPos.add(skuInfoPo);

                }
            }
            goodSkuPo.setSkuInfoList(skuInfoPos);
        }
        // 获取最小值
        if (priceList.size() > 0) {
            BigDecimal minPrice = Collections.min(priceList);
            goodsOutVo.setMinPrice(minPrice);
        }

        goodsOutVo.setGoodSkus(goodSkuPoList);
        return goodsOutVo;
    }

    @Override
    public BigDecimal getMaxAddPrice(Integer goodsId) {
        // 获取最高加价
        BigDecimal addPrice = BigDecimal.ZERO;

        QueryWrapper<BatchGoods> wrapper2 = new QueryWrapper<>();
        wrapper2.inSql("second_bantch_num",
                "select second_bantch_num from benefit_cards_info where balance > 0 and is_enable = 0 and user_id = '"
                        + TokenService.getUserBytoken().getId() + "'");
        wrapper2.eq("goods_id", goodsId);
        List<BatchGoods> batchGoods = this.baseMapper.getGoodsMaxPrice(wrapper2);
        batchGoods.remove(null);
        if (batchGoods.size() > 0) {
            addPrice = batchGoods.get(0).getAddPrice();
        }
        return addPrice;
    }

    @Override
    public IPage<GoodsPo> getTopicGoodsList(GoodsIdsInVo inVo, Long pageNo, Long pageSize) {

        // 获得该用户可以购买的商品合集
        String goodsIds = "-1";
        if (inVo.getGoodsIds() != null) {
            StringBuilder goodsIdsBuilder = new StringBuilder();
            goodsIdsBuilder.append(goodsIds);
            inVo.getGoodsIds().forEach(a -> {
                goodsIdsBuilder.append(",").append(a.getId());
            });
            goodsIds = goodsIdsBuilder.toString();
        }

        log.info(goodsIds);

        LambdaQueryWrapper<CompanySkuCharge> wrapper3 = new LambdaQueryWrapper<>();
        wrapper3.eq(CompanySkuCharge::getCompanyId, TokenService.getUserBytoken().getCompanyId())
                .inSql(CompanySkuCharge::getGoodsId, goodsIds);
        List<CompanySkuCharge> chargeList = companySkuChargeMapper.selectList(wrapper3);
        Map<Integer, BigDecimal> chargeMap = null;
        if (chargeList.size() > 0) {
            chargeMap = chargeList.stream().collect(Collectors.toMap(CompanySkuCharge::getSkuInfoId,
                    CompanySkuCharge::getMallAddPrice));
        }

        Integer distributorId = TokenService.getUserBytoken().getCompanyId();
        QueryWrapper<GoodsInfo> wrapper = new QueryWrapper<>();
        wrapper.inSql("id", goodsIds);
        wrapper.eq("distributorId", distributorId);

        Page<GoodsPo> page = new Page<>(pageNo, pageSize);
        IPage<GoodsPo> iPage = this.baseMapper.getWxGoodsListPageByConditions_new(page, wrapper);

        BigDecimal price;
        BigDecimal addMallPrice;
        // 计算最高价
        for (GoodsPo a : iPage.getRecords()) {
            price = a.getAdvocatePrice() == null ? new BigDecimal(a.getPrice()) : new BigDecimal(a.getAdvocatePrice());
            addMallPrice = chargeMap == null ? BigDecimal.ZERO : chargeMap.getOrDefault(a.getSkuId(), BigDecimal.ZERO);

            a.setPrice(price.add(addMallPrice).toString());
            // 输出日志
            if (StringUtils.hasLength(a.getAdvocatePrice())) {
                log.info("商品：" + a.getGoodsName() + ",goodsId:" + a.getGoodId() + ",建议售价不为空，取建议售价");
                log.info("商品：" + a.getGoodsName() + ",goodsId:" + a.getGoodId() + ",最终售价是：" +
                        "最小建议售价(" + a.getAdvocatePrice() + ")+商城加价(" + addMallPrice + ")=" + a.getPrice() + "元");
            } else {
                log.info("商品：" + a.getGoodsName() + ",goodsId:" + a.getGoodId() + ",建议售价为空！取结算成本");
                log.info("商品：" + a.getGoodsName() + ",goodsId:" + a.getGoodId() + ",最终售价是：" +
                        "最小结算成本(" + a.getPrice() + ")+商城加价(" + addMallPrice + ")=" + a.getPrice() + "元");
            }
        }

        return iPage;
    }

    @Override
    public IPage<CompanyGoodsOutVo> getGoodsBySaler(AllGoodsInVo inVo, Long pageNo, Long pageSize) {
        QueryWrapper<GoodsInfo> wrapper = new QueryWrapper<>();
        wrapper.like(StringUtils.hasLength(inVo.getGoodsName()), "goods_name", inVo.getGoodsName())
                .like(inVo.getGoodsId() != null, "id", inVo.getGoodsId())
                .like(StringUtils.hasLength(inVo.getVendorName()), "vendorName", inVo.getVendorName())
                .ge(inVo.getMinStock() != null, "stock", inVo.getMinStock())
                .le(inVo.getMaxStock() != null, "stock", inVo.getMaxStock())
                .ge(inVo.getMinSkuPrice() != null, "minprice", inVo.getMinSkuPrice())
                .le(inVo.getMaxSkuPrice() != null, "maxprice", inVo.getMaxSkuPrice())
                .ge(inVo.getMinAdvocatePrice() != null, "minAdvocatePrice", inVo.getMinAdvocatePrice())
                .le(inVo.getMaxAdvocatePrice() != null, "maxAdvocatePrice", inVo.getMaxAdvocatePrice())
                .eq(inVo.getGoodsType() != null, "goods_type", inVo.getGoodsType())
                .eq(inVo.getStatusSysUp() != null, "status_sys_up", inVo.getStatusSysUp())
                .eq(inVo.getStatusVerify() != null, "status_verify", inVo.getStatusVerify());
        wrapper.ne("status_verify", -1);
        wrapper.inSql("id",
                "select goods_id as id from g_company_goods where deleted = 0 and company_id = " + TokenService.getUserBytoken().getCompanyId());
        wrapper.eq("status_sys_up", 1)
                .eq("status_verify", 1);

        Page<CompanyGoodsOutVo> page = new Page<>(pageNo, pageSize);
        IPage<CompanyGoodsOutVo> iPage = this.baseMapper.getAllListPageByConditions_saler(page, wrapper);

        return iPage;
    }

}
